package com.niiwoo.civet.account.service.local.withdraw;/**
 * Created by DELL on 2017/12/7.
 */

import com.niiwoo.civet.account.dao.entity.WithdrawConfirmOrder;
import com.niiwoo.civet.account.dao.entity.WithdrawConfirmOrderExample;
import com.niiwoo.civet.account.dao.entity.WithdrawOrderAudit;
import com.niiwoo.civet.account.dao.entity.WithdrawOrderAuditExample;
import com.niiwoo.civet.account.dao.mapper.WithdrawOrderAuditMapper;
import com.niiwoo.tripod.provider.exception.BizException;
import com.niiwoo.tripod.service.component.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 提现订单审核
 *
 * @author xiaoyingping
 * @create 2017-12-07 10:25
 **/
@Service
public class WithdrawOrderAuditService {

    @Autowired
    private WithdrawOrderAuditMapper withdrawOrderAuditMapper;
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;

    /**
     * 通过提现订单ID查询审核记录
     * @param withdrawOrderId
     * @return
     */
    public WithdrawOrderAudit getByWithdrawOrderId(Long withdrawOrderId){
        Assert.notNull(withdrawOrderId, "提现订单ID不能为空");

        WithdrawOrderAuditExample example = new WithdrawOrderAuditExample();
        WithdrawOrderAuditExample.Criteria criteria = example.createCriteria();
        criteria.andWithdrawOrderIdEqualTo(withdrawOrderId);
        List<WithdrawOrderAudit> withdrawOrderAudits = withdrawOrderAuditMapper.selectByExample(example);

        WithdrawOrderAudit withdrawOrderAudit = null;
        if(!CollectionUtils.isEmpty(withdrawOrderAudits)){
            withdrawOrderAudit = withdrawOrderAudits.get(0);
        }
        return withdrawOrderAudit;
    }

    /**
     * 新增审核记录
     * @param withdrawOrderAudit
     */
    public void addWithdrawOrderAudit(WithdrawOrderAudit withdrawOrderAudit){
        WithdrawOrderAudit record = this.getByWithdrawOrderId(withdrawOrderAudit.getWithdrawOrderId());
        if(Objects.isNull(record)){
            withdrawOrderAudit.setId(snowflakeIdWorker.nextId());
            Date date = new Date();
            withdrawOrderAudit.setCreateTime(date);
            withdrawOrderAudit.setUpdateTime(date);
            withdrawOrderAuditMapper.insertSelective(withdrawOrderAudit);
        }
    }

}
